package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.pojo.Books;
import com.kuang.pojo.supplier;
import com.kuang.service.supplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.krb5.internal.PAForUserEnc;

import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    @Qualifier("supplierService")
    private  com.kuang.service.supplierServiceImpl supplierService;

    @RequestMapping("/getsupplier")
  public String getSupplier(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,@RequestParam(required=true,value="pageSize",defaultValue="6")  Integer pageSize, Model model) {
        PageHelper.startPage(pageNum,pageSize);
      List<supplier> supplier = supplierService.getSupplier();
      for (com.kuang.pojo.supplier supplier1 : supplier) {
          System.out.println(supplier1.toString());
      }


        PageInfo<supplier> pageInfo = new PageInfo<>(supplier);
        if (pageNum==0 ||pageNum>pageInfo.getPages()){

            return "redirect:/supplier/getsupplier";

        }

       model.addAttribute("pageInfo",pageInfo);
      return "supplier";
  }
  //添加数据
    @RequestMapping("/addsuplier")
    public String  addSupplier(supplier supplier){

        supplierService.addSupplier(supplier);
        return "redirect:/supplier/getsupplier";
    }
    //跳转到增加数据页面
    @RequestMapping("/redirctsuplier")
   public String redirctAdd(){
        return "addsuplier";
    }
//根据id查询供应商信息 且跳转到修改信息界面
    @RequestMapping("/getsupplierById")
    public String getsupplierById(int id,Model model){
        supplier supplier = supplierService.getsupplierById(id);
        model.addAttribute("supplier",supplier);
        return "updateSuppiler";
    }
//修改供应商信息  并且返回供应商页面
    @RequestMapping("/updateSupplier")
    public String updateSupplier(supplier supplier){
        supplierService.updateSupplier(supplier);
      return "redirect:/supplier/getsupplier";
    }
    //删除供应商根据id
    @RequestMapping("deletesupplierById")
    public String deletesupplierById(int id){
        supplierService.deletesupplierById(id);
        return "redirect:/supplier/getsupplier";
    }
    //根据公司名称进行模糊查询
    //根据公司名称模糊查询
    @RequestMapping("/getsupplierByName")
     public String getsupplierByName(String company,Model model){
         List<supplier> suppliers1 = supplierService.getsupplierByName(company);
         model.addAttribute("suppliers1",suppliers1);
         return "bynamesupplier";
    }
}

